<template>
  <el-dialog v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}字典`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="字典名称" prop="dictLabel">
        <el-input v-model="drawerProps.row!.dictLabel" placeholder="请填写字典名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="字典内容" prop="dictValue">
        <el-input v-model="drawerProps.row!.dictValue" placeholder="请填写字典内容" type="number" clearable></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="drawerProps.row!.sort" placeholder="请填写排序" type="number" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="drawerProps.row!.remarks" type="textarea" placeholder="备注" clearable maxlength="100" show-word-limit>
        </el-input>
        <el-color-picker v-model="drawerProps.row!.remarks" :predefine="colorList" />
      </el-form-item>
      <el-divider content-position="left"></el-divider>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="drawerProps.row!.unit" type="text" placeholder="单位" clearable> </el-input>
      </el-form-item>
      <el-form-item label="参数" prop="param">
        <el-input v-model="drawerProps.row!.param" type="text" placeholder="参数" clearable> </el-input>
      </el-form-item>
      <el-form-item label="最小值" prop="minValue">
        <el-input v-model="drawerProps.row!.minValue" type="text" placeholder="最小值" clearable> </el-input>
      </el-form-item>
      <el-form-item label="最大值" prop="maxValue">
        <el-input v-model="drawerProps.row!.maxValue" type="text" placeholder="最大值" clearable> </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
// import { genderType } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { System } from "@/api/interface";
import { DEFAULT_PRIMARY } from "@/config";

const rules = reactive({});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<System.ResDictList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

// 预定义主题颜色
const colorList = [
  DEFAULT_PRIMARY,
  "#daa96e",
  "#0c819f",
  "#409eff",
  "#27ae60",
  "#ff5c93",
  "#e74c3c",
  "#fd726d",
  "#f39c12",
  "#9b59b6"
];

defineExpose({
  acceptParams
});
</script>
